<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：loan.class.php
 * @类功能: 贷款产品
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\credit\controller;

use app\common\cache\loan\ILoanCache;
use app\common\cache\user\IUserCache;
use app\common\controller\CreditBaseController;
use app\common\enum\IsTrack;
use app\common\enum\LoanApplyStatus;
use app\common\ext\IDb;
use app\common\ext\ILog;

class Loan extends CreditBaseController {
    /**
     * @功能：取得贷款申请记录
     * @开发者：cxl
     */
    public function loan_apply_list(){
        //接收参数
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $loan_apply_where['agent_admin_id'] = self::$uid;
        if(!empty($bottom_id)) $loan_apply_where['loan_apply_id'] = array("lt",$bottom_id);
        $loan_apply_list = IDb::getInstance("loan_apply")
            ->setDbFiled("loan_apply_id,loan_product_id,user_id,apply_money,apply_period,loan_apply_status,loan_apply_time,loan_audit_time,loan_audit_note")
            ->setDbWhere($loan_apply_where)
            ->setDbOrder("loan_apply_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断列表是否存在
        if(empty($loan_apply_list) || !is_array($loan_apply_list) || count($loan_apply_list) <= 0){
            $loan_apply_list = array();
        }

        //整理数据
        foreach($loan_apply_list as &$data){
            //设置查询条件
            $loan_product_info = ILoanCache::getInfo($data['loan_product_id']);
            if(empty($loan_product_info['loan_product_id'])){
                self::set_code(self::WARNING_0002);
                self::set_msg("取得贷款申请记录失败！");
                self::send();
            }
//            //设置贷款申请状态
//            switch($data['loan_apply_status']){
//                case LoanApplyStatus::apply:
//                    $data['loan_apply_status_step'] = "0";
//                    $data['loan_apply_status_name'] = "申请已提交";
//                    break;
//                case LoanApplyStatus::audit_pass:
//                    $data['loan_apply_status_step'] = "2";
//                    $data['loan_apply_status_name'] = "材料审核通过";
//                    break;
//                case LoanApplyStatus::audit_failure:
//                    $data['loan_apply_status_step'] = "2";
//                    $data['loan_apply_status_name'] = "材料审核失败";
//                    break;
//                case LoanApplyStatus::confirm_loan:
//                case LoanApplyStatus::brokerage_money:
//                case LoanApplyStatus::finance_brokerage_money:
//                case LoanApplyStatus::apply_bak:
//                    $data['loan_apply_status_step'] = "3";
//                    $data['loan_apply_status_name'] = "申请成功";
//                    break;
//                default:
//                    self::set_code(self::WARNING_0004);
//                    self::set_msg("取得贷款申请记录失败！");
//                    self::send();
//                    break;
//            }

            //设置贷款机构名称
            $data['apply_money'] = number_format($data['apply_money'], 2);
            $data['loan_product_name'] = "{$loan_product_info['loan_agency_name']}-{$loan_product_info['loan_product_name']}";
//            $data['loan_agency_thumb'] = $loan_agency_info['thumb'];

            $data['loan_apply_time'] = substr($data['loan_apply_time'],0,16);

            $user_info = IUserCache::getInfo($data['user_id'],['real','phone']);
            $data['real'] = $user_info['real'];
            $data['loan_phone'] = $user_info['phone'];

            $data['not_log_day'] = 0;

            $log_where['loan_apply_id'] = $data['loan_apply_id'];
            $log_where['is_track'] = IsTrack::Yes;
            $log_info = IDb::getInstance("loan_apply_log")
                ->setDbFiled("operate_time")
                ->setDbWhere($log_where)
                ->setDbOrder('loan_apply_log_id desc')
                ->row();
            $time = strtotime($log_info['operate_time']);

            if(!empty($log_info) && $time < time()){
                $diff = intval((time()-$time)/3600/24);
                if($diff >= 3){
                    $data['not_log_day'] = $diff;
                }
            }
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_apply_list",$loan_apply_list);
        self::send();
    }

    /**
     * @功能：贷款产品进度查询日志
     * @开发者：cxl
     */
    public function loan_apply_log(){
        //接收参数
        $loan_apply_id  = self::get_data('loan_apply_id',self::NOT_EMPTY,"贷款产品申请编码不能为空！");
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $loan_apply_log_where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_log_where['is_track'] = IsTrack::Yes;
        if(!empty($bottom_id)) $loan_apply_log_where['loan_apply_log_id'] = array("lt",$bottom_id);
        $loan_apply_log_list = IDb::getInstance("loan_apply_log")
            ->setDbFiled("loan_apply_log_id,operate_note,operate_time")
            ->setDbWhere($loan_apply_log_where)
            ->setDbOrder("loan_apply_log_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($loan_apply_log_list as $key=>$value){
            $loan_apply_log_list[$key]['video_list'] = array();
            $loan_apply_log_list[$key]['audio_list'] = array();
            $loan_apply_log_list[$key]['img_list'] = array();
        }

        //判断列表是否存在
        if($loan_apply_log_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("贷款产品进度查询日志失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_apply_log",$loan_apply_log_list);
        self::send();
    }

    /**
     * @功能：贷款进度添加
     * @开发者：gys
     */
    public function apply_log_add(){
        //接收参数
        $loan_apply_id  = self::get_data('loan_apply_id',self::NOT_EMPTY,"申请编号不能为空！");
        $note  = self::get_data('note',self::NOT_EMPTY,"内容不能为空");

        $where['loan_apply_id'] = $loan_apply_id;
        $where['agent_admin_id'] = self::$uid;
        $apply_info = IDb::getInstance('loan_apply')
            ->setDbWhere($where)
            ->row();
        if(empty($apply_info)){
            self::set_code(self::ERR_0001);
            self::set_msg("申请不存在！");
            self::send();
        }

        $result = ILog::LoanApplyLog($loan_apply_id,null,null,self::$uid,$note,IsTrack::Yes);
        if(!$result){
            self::set_code(self::ERR_0001);
            self::set_msg("添加日常跟踪失败！");
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
